package com.sgb.kjwl.view.ui.activity.self.userInfo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.im.immodel.IMUtils;
import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.home.RefreshHomeNoticeEVent;
import com.sgb.kjwl.view.event.ChooseAreaEvent;
import com.sgb.kjwl.view.event.LoginEvent;
import com.sgb.kjwl.view.event.self.AgentApplyEvent;
import com.sgb.kjwl.view.event.self.ChangePwdEvent;
import com.sgb.kjwl.view.event.self.MachinePublishEvent;
import com.sgb.kjwl.view.ui.activity.MainActivity;
import com.sgb.link.utils.Preferences;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.event.EventConstant;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.util.UserOperating;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.jpush.android.api.JPushInterface;

/**
 * 修改密码全局弹框界面
 * Create by 2022/2/16 Lw
 */
public class ChangePasswordActivity extends Activity {

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, ChangePasswordActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 去除Activity的标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.at_change_password);

        // 设置宽度为屏幕的0.9
        WindowManager windowManager = getWindowManager();
        // 获取屏幕宽、高
        Display display = windowManager.getDefaultDisplay();
        // 获取对话框当前的参数值
        WindowManager.LayoutParams p = getWindow().getAttributes();
        // 宽度设置为屏幕的1
        p.width = (int) (display.getWidth() * 0.9);
        // 设置透明度,0.0为完全透明，1.0为完全不透明
        p.alpha = 0.95f;
        // 设置布局参数
        getWindow().setAttributes(p);

        // 设置点击弹框外部不可消失
        setFinishOnTouchOutside(false);

        initView();
    }

    private void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        TextView tv_confirm = (TextView) findViewById(com.swgk.core.R.id.tv_logout_confirm);
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginOutAndJump();
            }
        });
    }

    /**
     * 修改密码确认后，清空用户信息，并跳转登录界面
     */
    public void loginOutAndJump() {
        //退出登录接口保持和后端信息一致
        APIManager.getInstance().getUserAPI().logout().enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
            }
        });
        com.swgk.core.base.api.APIManager.getInstance().cancelOkHttpCalls(true);
        JPushInterface.deleteAlias(this, 1);
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_TOKEN, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PHONE, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.LOGIN_PC_TOKEN, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourcePCToken, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourceAPPToken, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ENTERPRISE_AUTH, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PERSON_AUTH, false);
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.DRIVER_AUTH, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NAME, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, "");
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COM_TYPE, 0);
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NO, "");
        //退出登录后清空用户状态信息
        IMUtils.getInstance().clearLoginUserInfo();
        EventBus.getDefault().post(MachinePublishEvent.make().setMessageTag(EventConstant.EVENT_LOGIN_OUT));
        EventBus.getDefault().postSticky(LoginEvent.make(EventConstant.EVENT_LOGIN_OUT, ""));
        EventBus.getDefault().postSticky(ChooseAreaEvent.make(EventConstant.EVENT_MENU_DEVICE_OUT, ""));
        //通知大首页刷新数据
        EventBus.getDefault().postSticky(new RefreshHomeNoticeEVent(EventConstant.EVENT_NOTICE_REFRESH_HOME));
        Preferences.saveUserToken("");
        Preferences.saveUserAccount("");
        //repertory.saveToken(null);
        Preferences.saveUserAccount("");
        Preferences.saveUserToken("");
        UserOperating.getInstance().cleanCacheUser();
        MkvUtils.get().clear(MkvUtils.CacheUserCenterMenu);
        //跳转登录界面
        VerificationCodeLoginActivity.start(this, EventConstant.EVENT_MAIN_ACTIVITY_JUMP);
        finish();
    }

    @Override
    public void onBackPressed() {
        //禁止返回操作
        //super.onBackPressed();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ChangePwdEvent event) {
        if ( "OK".equals(event.getResult())) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
